<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="提现人" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入提现人"
          clearable
          
        />
      </el-form-item>
   
      <el-form-item label="提现时间" prop="addtime">
        <el-input
          v-model="queryParams.addtime"
          placeholder="请输入提现时间"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="审核时间" prop="endTime">
        <el-input
          v-model="queryParams.endTime"
          placeholder="请输入审核时间"
          clearable
          
        />
      </el-form-item>

      <el-form-item label="提现状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择提现状态">
          <el-option label="全部" value=""></el-option>
          <el-option label="待审核" value="0"></el-option>
          <el-option label="审核通过" value="1"></el-option>
          <el-option label="审核不通过" value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="提现金额" prop="talMoney">
        <el-input
          v-model="queryParams.talMoney"
          placeholder="请输入提现金额"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="到账金额" prop="money">
        <el-input
          v-model="queryParams.money"
          placeholder="请输入到账金额"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="手续费" prop="sxf">
        <el-input
          v-model="queryParams.sxf"
          placeholder="请输入手续费"
          clearable
          
        />
      </el-form-item>

      <el-form-item label="提现姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入提现姓名"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="支付宝账号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入支付宝账号"
          clearable
          
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:withdraw:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:withdraw:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:withdraw:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:withdraw:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="withdrawList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="提现人" align="center" prop="userId" />
          <el-table-column label="用户" align="center" prop="username" width="200">
        <template slot-scope="scope">
          <div
            style=" display:flex;   align-items: center"
          >
            <el-avatar :src="scope.row.headimg"></el-avatar>
           <div>{{ scope.row.username }}</div> 
          </div>
        </template>
      </el-table-column>
      <el-table-column label="提现状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0">未审核</el-tag>
          <el-tag v-if="scope.row.status == 1" type="success">已通过</el-tag>
          <el-tag v-if="scope.row.status == 2" type="warning">未通过</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="提现金额" align="center" prop="talMoney" />
      <el-table-column label="到账金额" align="center" prop="money" />
      <el-table-column label="手续费" align="center" prop="sxf" />
      <el-table-column label="提现方式" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 1">微信</el-tag>
          <el-tag v-if="scope.row.type == 2" type="warning">银行卡</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="收款二维码" align="center" prop="imgurl" />
      <el-table-column label="提现姓名" align="center" prop="name" />
      <el-table-column label="支付宝账号" align="center" prop="number" />
      <!-- <el-table-column label="" align="center" prop="creator" /> -->

      <el-table-column
        label="提现时间"
        align="center"
        prop="addtime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.addtime, "{y}-{m}-{d} {hh}:{mm}:{ss}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="endTime" />
      <el-table-column label="原因" align="center" prop="reason" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="180"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 0"
         size="small"
           type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,1)"
            v-hasPermi="['system:withdraw:edit']"
            >通过</el-button
          >
          <el-button
            v-if="scope.row.status == 0"
         size="small"
           type="danger"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,2)"
            v-hasPermi="['system:withdraw:edit']"
            >拒绝</el-button
          >
            <el-tag v-if="scope.row.status == 1" type="success">已通过</el-tag>
          <el-tag v-if="scope.row.status == 2" type="warning">未通过</el-tag>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:withdraw:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="提现人" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入提现人" />
        </el-form-item>
        <el-form-item label="提现时间" prop="addtime">
          <el-input v-model="form.addtime" placeholder="请输入提现时间" />
        </el-form-item>
        <el-form-item label="审核时间" prop="endTime">
          <el-input v-model="form.endTime" placeholder="请输入审核时间" />
        </el-form-item>
        <el-form-item label="原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入原因" />
        </el-form-item>
        <el-form-item label="提现金额" prop="talMoney">
          <el-input v-model="form.talMoney" placeholder="请输入提现金额" />
        </el-form-item>
        <el-form-item label="到账金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入到账金额" />
        </el-form-item>
        <el-form-item label="手续费" prop="sxf">
          <el-input v-model="form.sxf" placeholder="请输入手续费" />
        </el-form-item>
        <el-form-item label="收款二维码" prop="imgurl">
          <el-input v-model="form.imgurl" placeholder="请输入收款二维码" />
        </el-form-item>
        <el-form-item label="提现姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入提现姓名" />
        </el-form-item>
        <el-form-item label="支付宝账号" prop="number">
          <el-input v-model="form.number" placeholder="请输入支付宝账号" />
        </el-form-item>
        <el-form-item label="" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="createDate">
          <el-date-picker
            clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWithdraw,
  getWithdraw,
  delWithdraw,
  addWithdraw,
  updateWithdraw,
} from "@/api/withdraw/list.js";
import { getMy_user } from "@/api/users/user.js";
export default {
  name: "Withdraw",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      withdrawList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        status: null,
        addtime: null,
        endTime: null,
        reason: null,
        talMoney: null,
        money: null,
        sxf: null,
        type: null,
        imgurl: null,
        name: null,
        number: null,
        creator: null,
        createDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        talMoney: [
          { required: true, message: "提现金额不能为空", trigger: "blur" },
        ],
        money: [
          { required: true, message: "到账金额不能为空", trigger: "blur" },
        ],
        sxf: [{ required: true, message: "手续费不能为空", trigger: "blur" }],
        type: [
          {
            required: true,
            message: "提现方式:1=微信,2=银行卡不能为空",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listWithdraw(this.queryParams).then((response) => {
        this.withdrawList = response.rows;
          this.queryUser(this.withdrawList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        status: 0,
        addtime: null,
        endTime: null,
        reason: null,
        talMoney: null,
        money: null,
        sxf: null,
        type: null,
        imgurl: null,
        name: null,
        number: null,
        creator: null,
        createDate: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row,type) {
      this.reset();
      const id = row.id || this.ids;

      getWithdraw(id).then((response) => {
        this.form = response.data;
        if(type==1){
          this.form.status=1;
        }else if(type==2){
            this.form.status=2;
        }

   
        updateWithdraw(this.form).then((response) => {
          this.$modal.msgSuccess("已通过");
        //   this.open = false;
          this.getList();
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateWithdraw(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWithdraw(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？')
        .then(function () {
          return delWithdraw(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/withdraw/export",
        {
          ...this.queryParams,
        },
        `withdraw_${new Date().getTime()}.xlsx`
      );
    },
     //查询用户信息
    async queryUser(arr) {
      let val = arr;
      for (let index = 0; index < arr.length; index++) {
        console.log(arr[index].userId);
        await getMy_user(arr[index].userId).then((response) => {
          if(response.data){
            this.$set(this.withdrawList[index], "username", response.data.nickname);
          this.$set(this.withdrawList[index], "headimg", response.data.headimg);
          }
          
        
        });
      }
    },
  },
};
</script>
